﻿using UnityEditor;
using UnityEngine;

namespace ChivalryUnityEditor
{
    [CustomEditor(typeof(Transform))]
    class TransformInspector : Editor
    {
        public const string KEY_CONSTRAINT_ROTATION = "TRANSFORM_CONSTRAINT_ROTATION";

        private Transform m_transform;
        private bool m_constraintRotation;

        private void OnEnable()
        {
            m_transform = target as Transform;
            m_constraintRotation = EditorPrefs.GetBool(KEY_CONSTRAINT_ROTATION, true);
        }

        private void OnDisable()
        {
            EditorPrefs.SetBool(KEY_CONSTRAINT_ROTATION, m_constraintRotation);
        }

        public override void OnInspectorGUI()
        {
            bool reset;

            //Position
            EditorGUILayout.BeginHorizontal();
            reset = GUILayout.Button("P", GUILayout.MaxWidth(20));
            Vector3 localPosition = m_transform.localPosition;
            //限定两位小数
            localPosition.x = MathUtil.Round(localPosition.x, 2);
            localPosition.y = MathUtil.Round(localPosition.y, 2);
            localPosition.z = MathUtil.Round(localPosition.z, 2);
            m_transform.localPosition = EditorGUILayout.Vector3Field(GUIContent.none, localPosition);
            //reset
            if (reset)
            {
                m_transform.localPosition = Vector3.zero;
            }
            EditorGUILayout.EndHorizontal();

            //Rotation
            EditorGUILayout.BeginHorizontal();
            reset = GUILayout.Button("R", GUILayout.MaxWidth(20));
            Vector3 localEulerAngles = m_transform.localEulerAngles;
            if (m_constraintRotation)
            {
                localEulerAngles.x = MathUtil.NormalizeAngle180(localEulerAngles.x);
                localEulerAngles.y = MathUtil.NormalizeAngle180(localEulerAngles.y);
                localEulerAngles.z = MathUtil.NormalizeAngle180(localEulerAngles.z);
            }
            localEulerAngles.x = MathUtil.Round(localEulerAngles.x, 2);
            localEulerAngles.y = MathUtil.Round(localEulerAngles.y, 2);
            localEulerAngles.z = MathUtil.Round(localEulerAngles.z, 2);
            m_transform.localEulerAngles = EditorGUILayout.Vector3Field("", localEulerAngles);
            //reset
            if (reset)
            {
                m_transform.localRotation = Quaternion.identity;
            }
            EditorGUILayout.EndHorizontal();

            //Scale
            EditorGUILayout.BeginHorizontal();
            reset = GUILayout.Button("S", GUILayout.MaxWidth(20));
            Vector3 localScale = m_transform.localScale;
            localScale.x = MathUtil.Round(localScale.x, 2);
            localScale.y = MathUtil.Round(localScale.y, 2);
            localScale.z = MathUtil.Round(localScale.z, 2);
            m_transform.localScale = EditorGUILayout.Vector3Field("", localScale);
            //reset
            if (reset)
            {
                m_transform.localScale = Vector3.one;
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space();
            m_constraintRotation = EditorGUILayout.ToggleLeft(new GUIContent("ConstraintRotation", "将旋转约束在(-180,180)范围内.若进行约束可能会影响动画制作"), m_constraintRotation);
        }
    }
}
